//
//  HWShiWuShangPingViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/10/9.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWShiWuShangPingViewController.h"
#import "HWShiWuCollectionViewCell.h"
#import "HWShangpingxiangqingViewController.h"
#import "HWXuGoodsNiModel.h" // 模型数据



@interface HWShiWuShangPingViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(strong,nonatomic) UICollectionView *maincollectionView;

@property(strong,nonatomic)  NSString *p;
@property(strong,nonatomic)  NSString *max_p;
@property(strong,nonatomic)  NSMutableArray *dataMuArr;




@end

@implementation HWShiWuShangPingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
}

- (void)setupUI {
    
     self.view.backgroundColor = RGB(242, 242, 242);
    
    [self addControlsview];
    
    [self addrefreshControl];
    
}


#pragma  mark -----  添加控件
- (void)addControlsview {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 1;
    
 //   CGFloat juanju  = (kScreenW - 24*CFLScaleValue*2-62*CFLScaleValue*3-78*CFLScaleValue)/2;
    layout.minimumInteritemSpacing = 1;
  //  layout.headerReferenceSize = CGSizeMake(kScreenW, 142);  //设置headerView大小
    self.maincollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH- 165-CF_StatusBarHeight-20-8-40) collectionViewLayout:layout];
    // self.maincollectionView.showsHorizontalScrollIndicator = NO;
    self.maincollectionView.delegate = self;
    self.maincollectionView.dataSource = self;
    self.maincollectionView.alwaysBounceVertical = YES;
  //  self.maincollectionView.backgroundColor = [UIColor whiteColor];
    
    self.maincollectionView.backgroundColor =RGB(242, 242, 242);
   // self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 15, 0, 15);
  //  self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 24*CFLScaleValue, 0, 24*CFLScaleValue);
  //  [self.maincollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];  //  一定要设置
    [self.maincollectionView registerClass:[HWShiWuCollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
  //  [self.maincollectionView  registerNib:[UINib nibWithNibName:@"HWJinXuanCell" bundle:nil] forCellWithReuseIdentifier:@"cellID"];
    
    self.maincollectionView.userInteractionEnabled = YES;
    [self.view  addSubview:self.maincollectionView];
    
    
    
}




#pragma  mark ---- 添加刷新

- (void)addrefreshControl {
    
    self.maincollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    
   [self.maincollectionView.mj_header beginRefreshing];
    
    self.maincollectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
}

// 下拉刷新
- (void)underRefresh {
    
   
    
    self.p = @"1";
     
    NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    
    NSString *typeStr =  [NSString stringWithFormat:@"%@",@"1"];
    
    NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
    
    NSString *singMD5 = [WMUtil md5HexDigest:singStr];

    self.dataMuArr = [[NSMutableArray alloc]init];
    NSDictionary *parametes = @{
                                       @"p" :self.p,
                                       @"ios" : @"1",
                                       @"ct" :  @"app",
                                       @"ac" : @"mall",
                                       @"uid" :uidStr,
                                       @"t" : tStr,
                                       @"type" :typeStr,
                                       @"sign" : singMD5
                                       };
       
       [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
           
           // [SVProgressHUD dismiss];
           [self.maincollectionView.mj_header endRefreshing];
           [self.maincollectionView.mj_footer endRefreshing];
           
           
           NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
           
           if ([codeStr intValue] == 1) {
               
               self.dataMuArr = [HWXuGoodsNiModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
               self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
               self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
               
               [self.maincollectionView reloadData];
               
    
           }
        
           
           
       } failure:^(NSString *error) {
           [self.maincollectionView.mj_header endRefreshing];
           [self.maincollectionView.mj_footer endRefreshing];
           
           
       }];
    
    
}

// 上拉加载

- (void)onRefresh {
    
    
   
    int p = [self.p intValue];
                   
    int max_p = [self.max_p intValue];
                   
     if (p > max_p) {
        [MBProgressHUD showError:@"没有更多数据，请待会在上拉加载"];
        [self.maincollectionView.mj_header endRefreshing];
        [self.maincollectionView.mj_footer endRefreshing];
        return ;
         
         
                       
      }
                   
    
    
      NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
      NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
     
      NSString *typeStr =  [NSString stringWithFormat:@"%@",@"1"];
     
      NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
     
     NSString *singMD5 = [WMUtil md5HexDigest:singStr];

      NSDictionary *parametes = @{
                                          @"p" :self.p,
                                          @"ios" : @"1",
                                          @"ct" :  @"app",
                                          @"ac" : @"mall",
                                          @"uid" :uidStr,
                                          @"t" : tStr,
                                          @"type" :typeStr,
                                          @"sign" : singMD5
                                          };
         
        [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
             
             // [SVProgressHUD dismiss];
             [self.maincollectionView.mj_header endRefreshing];
             [self.maincollectionView.mj_footer endRefreshing];
             
             
             NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
             
             if ([codeStr intValue] == 1) {
                 
                 NSMutableArray *mutableArr = [HWXuGoodsNiModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];


                 for (int i = 0; i < mutableArr.count; i++) {
                     [self.dataMuArr addObject:mutableArr[i]];

                 }
                 
                 self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                 self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                 [self.maincollectionView reloadData];
                 
      
             }
          
             
             
         } failure:^(NSString *error) {
             
             [self.maincollectionView.mj_header endRefreshing];
             [self.maincollectionView.mj_footer endRefreshing];
            
         }];
      
    
}




#pragma mark --------- UICollectionViewDelegate


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
 
    return self.dataMuArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HWShiWuCollectionViewCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    cell.backgroundColor = [UIColor whiteColor];
    cell.model = self.dataMuArr[indexPath.row];
    
    
    return cell;
    
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake((SCREEN_WIDTH-1)/2,198);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    DLog(@"12334");
    
     HWXuGoodsNiModel *model =  self.dataMuArr[indexPath.row];
    
    HWShangpingxiangqingViewController *shangping = [[HWShangpingxiangqingViewController alloc]init];
    shangping.goodsID = model.goods_id;
    [self.navigationController pushViewController:shangping animated:YES];
    
    
}






@end
